package com.ruoyi.common.utils.bootdo;


import com.ruoyi.project.wx.domain.SetWechatDO;
import org.jdom2.JDOMException;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by HP on 2019/7/13.
 */
public class WeChatPayUtils {

    private static final String URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    /*private static final String APP_ID = "wx0fd5a1a2a25623da";
    private static final String MCH_ID = "1512710461";
    private static final String TRADE_TYPE = "JSAPI";
    private static final String SIGN_TYPE = "MD5";
    private static final String CHARSET = "UTF-8";

    private static String Key = "we8ewbk02fz4xbrnzq2up751ymy5ds32";*/

    /**
     * @param args
     */
    /*public static void main(String[] args) {
//        System.out.println(MD5Util.getRandomStringByLength(32));
//        System.out.println(MD5Util.getRandomStringByLength(32).length());
        SortedMap<String ,String> map = new TreeMap<>();
        map.put("body", "套餐名称"); //商品描述  必
        //map.put("attach", "套餐附加信息"); //商品详情  非
        map.put("out_trade_no", "20879403372943360"); //商户订单号  必
        map.put("total_fee", "1"); //总金额  必
        map.put("spbill_create_ip", "182.119.183.6"); //终端ip  用户端ip  必
        WeixinpayService wxService = new WeixinpayService();
        WeixinOrder resultMap =wxService.getOrderSignature(map);
    }*/

    /**
     * 微信支付签名算法sign
     * @param characterEncoding
     * @param parameters
     * @return
     */
    public static String createSign(String characterEncoding, SortedMap<Object,Object> parameters, SetWechatDO setWechatDO){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();//所有参与传参的参数按照accsii排序（升序）
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + setWechatDO.getKey());
        System.out.println("拼接信息:"+sb);
        String sign = MD5Utils.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }



/**
 * AlipaySignature.rsaSign(order.getMapString(), APP_PRIVATE_KEY, CHARSET, SIGN_TYPE); 签名
 * AlipaySignature.rsaCheckV1()验签
 */
    /**
     * @param map
     * @return
     */
    public static Map<String,Object> signature(SortedMap map,SetWechatDO setWechatDO) {
        String sign =  createSign(setWechatDO.getCharset(),map,setWechatDO);
        String xml ="<xml>\n" +
                "   <appid>"+map.get("appid")+"</appid>\n" +
                "   <attach>"+map.get("attach")+"</attach>" +
                "   <body>"+map.get("body")+"</body>" +
                "   <mch_id>"+map.get("mch_id")+"</mch_id>" +
                "   <nonce_str>"+map.get("nonce_str")+"</nonce_str>" +
                "   <notify_url>"+map.get("notify_url")+"</notify_url>" +
                "   <openid>"+map.get("openid")+"</openid>" +
                "   <out_trade_no>"+map.get("out_trade_no")+"</out_trade_no>" +
                "   <spbill_create_ip>"+map.get("spbill_create_ip")+"</spbill_create_ip>" +
                "   <time_start>"+map.get("time_start")+"</time_start>" +
                "   <total_fee>"+map.get("total_fee")+"</total_fee>" +
                "   <trade_type>"+map.get("trade_type")+"</trade_type>" +
                "   <sign>"+sign+"</sign>" +
                "</xml>";
        String result = PayCommonUtil.httpsRequest(
                URL, "POST", xml);
        Map<String, String> resultMap = null;
        try {
            resultMap = PayCommonUtil.doXMLParse(result);
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        SortedMap<Object, Object> parameterMap = new TreeMap<Object, Object>();

        /*parameterMap.put("appid", setWechatDO.getAppId());
        parameterMap.put("noncestr", resultMap.get("nonce_str"));
        parameterMap.put("package", "Sign=WXPay");
        parameterMap.put("partnerid", setWechatDO.getMchId());
        parameterMap.put("prepayid", resultMap.get("prepay_id"));
        parameterMap.put("timestamp", System.currentTimeMillis()/1000);
        String sign2 = createSign(setWechatDO.getCharset(), parameterMap,setWechatDO);
        //parameterMap.put("sign", sign2);
        //jsonObject.put("parameterMap",parameterMap);
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("appId", setWechatDO.getAppId());
        map2.put("partnerId",setWechatDO.getMchId());
        map2.put("prepayId",parameterMap.get("prepayid").toString());
        map2.put("nonceStr", parameterMap.get("noncestr").toString());
        map2.put("timeStamp", parameterMap.get("timestamp").toString());
        map2.put("sign", sign2);*/
        parameterMap.put("appId", setWechatDO.getAppId());
        parameterMap.put("timeStamp", System.currentTimeMillis()/1000);
        parameterMap.put("nonceStr", resultMap.get("nonce_str"));
        parameterMap.put("signType", "MD5");
        parameterMap.put("package", "prepay_id="+resultMap.get("prepay_id"));
        String sign2 = createSign(setWechatDO.getCharset(), parameterMap,setWechatDO);
        //parameterMap.put("sign", sign2);
        //jsonObject.put("parameterMap",parameterMap);
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("appId", setWechatDO.getAppId());
        map2.put("partnerId",setWechatDO.getMchId());
        map2.put("prepayId",parameterMap.get("package").toString());
        map2.put("nonceStr", parameterMap.get("nonceStr").toString());
        map2.put("timeStamp", parameterMap.get("timeStamp").toString());
        map2.put("sign", sign2);
        return map2 ;
    }


    public static Map<String,Object> getOrderSignature(SortedMap map,SetWechatDO setWechatDO) {
        //WeixinOrder map = new WeixinOrder(APP_ID,MCH_ID,Json.toJson(order,JsonFormat.compact()),SIGN_TYPE) ;//new WeixinOrder(APP_ID, Json.toJson(order, JsonFormat.compact()));
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        map.put("nonce_str",MD5Utils.getRandomStringByLength(32));
        //map.setNonce_str(MD5Util.getRandomStringByLength(32));
        map.put("time_start",df.format(new Date()));
        //map.setTime_start(df.format(new Date()));
        map.put("notify_url",setWechatDO.getUrl());
        //map.setNotifyUrl("https://www.meipinjie.net/meipin/open/api/pay/weixinpayNotify");
        map.put("appid", setWechatDO.getAppId()); //应用ID  必
        map.put("mch_id", setWechatDO.getMchId()); //商户号  必
        //map.put("sign_type", "MD5"); //签名类型默认MD5
        //.put("notify_url", order.getTotalAmount()); //通知地址 回调url  必
        map.put("trade_type", setWechatDO.getTradeType()); //交易类型  APP 必
        //map.put("time_start", order.getTotalAmount()); //交易开始时间  必
        //signMap.put("time_expire", order.getTotalAmount()); //交易结束时间    大于5分钟
        return signature(map,setWechatDO);
    }

}
